
function page(divId,sql,start,offset,total,func_dwr){
    var c = sql + " limit " + start + "," + offset;
	func_dwr(c);
	var divObj = document.getElementById(divId);
	
	$j(divObj).find("#pageDivcontainer").remove();
	
	var divcontainer = "<div id='pageDivcontainer'></div>";
	$j(divObj).html($j(divObj).html()+ divcontainer);
	if(!document.getElementById("pageSelect")){
		
		var selectPage  = document.createElement('select');
		selectPage.setAttribute('id','pageSelect');
		for(var i=0;i<Math.ceil(total/offset);i++){
			selectPage.options[i] = new Option(i+1,i);
		}
		$j(selectPage).change(function(){
			selectChange(this,divId,sql,offset,total,func_dwr);
		});
		var theSelectedIndex = start/offset;
		selectPage.options[theSelectedIndex].selected = "selected";
		$j('#pageDivcontainer').append(selectPage);
	}else {
	}
	
	var lastRecordOfCurentPage = (eval(start+1+offset)>total)?total:eval(start+offset);
	$j('#pageSelect').before("<a>"+"第"+eval(start+1)+"-"+lastRecordOfCurentPage+"条&nbsp;"+"共"+total+"条&nbsp;"+"第"+eval(start/offset+1)+"页&nbsp;共"+Math.ceil(total/offset)+"页 &nbsp</a>");

	//$j('#pageSelect').before("<a>"+"页始/总数:"+eval(start+1)+"/"+total+"&nbsp; 每页显示数:"+offset+"&nbsp; 当前页/总共页:"+eval(start/offset+1)+"/"+Math.ceil(total/offset)+" &nbsp</a>");
	
	
	if(eval(start)==0){
		var aFirst = '<button  disabled="true"  >';
		aFirst += '首页</button>';
		$j('#pageSelect').before(aFirst);
		
		$j('#pageSelect').before("&nbsp;");
		 
		var aPrevious = '<button   disabled="true"  >';
		aPrevious += '上一页</button>';
		$j('#pageSelect').before(aPrevious);
		$j('#pageSelect').before("&nbsp;");
	}else{
		var aFirst  = document.createElement('button');
		aFirst.innerHTML = '首页';
		$j(aFirst).bind("click",function(){
			//alert(0);
			pageButtonClick(divId,sql,0,offset,total,func_dwr);
		});
		$j('#pageSelect').before($j(aFirst));
		$j('#pageSelect').before("&nbsp;");
		
		
		var checkPrevious = (eval(start-offset)>=0)?eval(start-offset):0;
		aPrevious = document.createElement('button');
		aPrevious.innerHTML = '上一页';
		$j(aPrevious).bind("click",function(){
			//alert(-1+"#"+checkPrevious);
			pageButtonClick(divId,sql,checkPrevious,offset,total,func_dwr);
		});
		$j('#pageSelect').before($j(aPrevious));
		$j('#pageSelect').before("&nbsp;");
		

	}
	
	if(eval(start)==eval((Math.ceil(total/offset)-1)*offset)){
		var aNext = '<button  disabled="true" >';
		aNext += '下一页</button>';
		$j('#pageSelect').before(aNext);
		$j('#pageSelect').before("&nbsp;");
		
		var aLast = '<button  disabled="true" >';
		aLast += '尾页</button>';
		$j('#pageSelect').before(aLast);
		$j('#pageSelect').before("&nbsp;");
	}else{
		var checkNext = (eval(start+offset)<=eval((Math.ceil(total/offset)-1)*offset))? eval(start+offset):eval((Math.ceil(total/offset)-1)*offset);
		aNext = document.createElement('button');
		aNext.innerHTML = '下一页';
		$j(aNext).bind("click",function(){
			//alert(-1+"#"+checkNext);
			pageButtonClick(divId,sql,checkNext,offset,total,func_dwr);
		});
		$j('#pageSelect').before($j(aNext));
		$j('#pageSelect').before("&nbsp;");
		
		
		var aLast = document.createElement('button');
		aLast.innerHTML = '尾页';
		
		$j(aLast).bind("click",function(){
			//alert(10000+"#"+eval((Math.ceil(total/offset)-1)*offset));
			pageButtonClick(divId,sql,eval((Math.ceil(total/offset)-1)*offset),offset,total,func_dwr);
		});
		$j('#pageSelect').before($j(aLast));
		$j('#pageSelect').before("&nbsp;");
		
	
	}
}
function pageButtonClick(divId,sql,start,offset,total,func_dwr){	
	page(divId,sql,start,offset,total,func_dwr);
}

function selectChange(obj,divId,sql,offset,total,func_dwr){
	page(divId,sql,obj.options[obj.options.selectedIndex].value * offset,offset,total,func_dwr);
} 



